﻿using UnityEngine;
using Network;
using Shipwar.SDK;
using LitJson;
using Framework;
using System.Collections.Generic;
using System.Collections;
using Framework.AssetsManager;

public class ProcLobby : Proc
{
    public ProcLobby()
        : base(ProcType.PROC_LOBBY)
    {

    }

    public override void process()
    {
        NetManager.Instance.Tick(Time.deltaTime);
    }

    public override void onBegin(object[] param)
    {
        RegistUIEvent();
        RegistNetworkEvent();

        Input.multiTouchEnabled = false;
        SceneManager.Instance.BeginLoadScene(new List<string>() { "lobby" }, () =>
        {
            IsInit = true;
            SceneManager.Instance.EndLoadScene();
        });
    }

    public override void onEnd()
    {
        UnregistUIEvent();
        UnregistNetworkEvent();

        Input.multiTouchEnabled = true;
    }

    void RegistUIEvent()
    {
    }

    void RegistNetworkEvent()
    {
        NetManager.Instance.tcpConc.connectionDelegate += onReconnectSuccess;
        NetManager.Instance.RegistMsg((ushort)JsonMsgId.JoinRoomResponse, onJoinRoom);
        NetManager.Instance.RegistMsg((ushort)JsonMsgId.LogonResponse, onReLogin);
    }

    void UnregistUIEvent()
    {
    }

    void UnregistNetworkEvent()
    {
        NetManager.Instance.UnregistMsg((ushort)JsonMsgId.JoinRoomResponse);
        NetManager.Instance.UnregistMsg((ushort)JsonMsgId.LogonResponse, onReLogin);
        NetManager.Instance.tcpConc.connectionDelegate -= onReconnectSuccess;
    }


    private void onJoinRoom(object msg)
    {
        JsonData json = (JsonData)msg;
        Debug.Log(json.ToJson());
        int code = (int)json["code"];
        switch(code)
        {
            case 0:
                uint conv = (uint)((int)json["conv"]);
                string host = (string)json["host"];
                ushort port = (ushort)((int)json["port"]);
                Mainloop.Instance.ctrl.changeProc(ProcType.PROC_GAME, host, port, conv);
                break;
            default:
                TipsPanel.Instance.Init("未知错误，错误码：" + code, null);
                break;
        }
    }


    public override void onTcpDisconnected()
    {
        NetManager.Instance.Connect(Config.JavaServerIp, Config.JavaTcpPort);
    }

    void onReconnectSuccess()
    {
        JsonData loginData = new JsonData();
        loginData["guid"] = SDK.GetInstance().currentUser.AppUID;
        loginData["token"] = SDK.GetInstance().currentUser.APPToken;
        loginData["channel"] = "JJ";
        loginData["version"] = Mainloop.Instance.AppVersion;
        loginData["firstLogon"] = false;
        NetManager.Instance.sendTcpMsg((ushort)JsonMsgId.LogonRequest, loginData.ToJson());
    }

    void onReLogin(object msg)
    {
        JsonData json = (JsonData)msg;
        int code = (int)json["code"];
        if (code == 0)
        {
            JsonData kcpState = json["kcpState"];
            if(kcpState == null)
            {
                //大厅中重新登陆做界面回复逻辑
            }
            else
            {
                string host = (string)kcpState["host"];
                ushort port = (ushort)kcpState["port"];
                uint conv = (uint)kcpState["conv"];
                Mainloop.Instance.ctrl.changeProc(ProcType.PROC_GAME, host, port, conv);
            }
        }
        else
        {
            Mainloop.Instance.ctrl.changeProc(ProcType.PROC_LOGIN);
        }
    }
    
}
